<template>
  <div class="add-role">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>{{roleId?'修改角色':'添加角色'}}</span>

      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="step-container">
        <el-steps direction="vertical" :active="activeIndex">
          <el-step title="角色信息" />
          <el-step title="权限信息" />
          <el-step title="检查并完成" />
        </el-steps>
      </div>
      <div class="form-container" v-show="activeIndex==1">
        <div class="title">角色信息</div>
        <div class="form">
          <el-form class="form-box" ref="roleForm" :model="roleForm" :rules="roleRules">
            <el-form-item label="角色名称" prop="roleName">
              <el-input v-model="roleForm.roleName" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="roleForm.remark" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container" v-show="activeIndex==2">
        <div class="title">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="tree"
                :data="item.children"
                show-checkbox
                default-expand-all
                node-key="id"
                highlight-current
                :props="{ label: 'title' }"
              />
            </div>
          </div>
        </div>
        <div class="form">
          权限配置内容
        </div>
      </div>
      <div class="form-container" v-show="activeIndex==3">
        <div class="title">检查并完成</div>
        <div class="form">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="disabletree"
                :data="item.children"
                show-checkbox
                default-expand-all
                node-key="id"
                highlight-current
                :props="{ label: 'title' ,
              disabled:()=>true}"
              />
            </div>
          </div>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="previous" v-show='activeIndex>1'>上一步</el-button>
        <el-button type="primary" @click="next" v-show='activeIndex<3'>下一步</el-button>
        <el-button type="primary" v-show='activeIndex==3' @click="confirmAdd" >{{ roleId?'修改角色':'添加角色' }}</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import {getTreeListAPI,addRoleAPI,getSearchListAPI,updateRoleAPI} from '@/api/system'
export default {
  data(){
    return{
      treeList:[],
      activeIndex:1,
      roleForm:{
        roleName:'',
        remark:'',
        perms:[]
      },
      roleRules:{
        roleName:[{required:true,message:'请输入角色名称',trigger:'blur'}]
      }
    }
  },
  created(){
    this.getTreeList()
  },
  computed:{
    roleId(){
     return this.$route.query.id
    }
  },
  mounted(){
    if(this.$route.query.id){
      this.getSearchList()
    }
  },
  methods:{
    async getSearchList(){
      const res=await getSearchListAPI(this.roleId)
      // console.log(res)
      const {perms,remark,roleId,roleName}=res.data
      this.roleForm = { perms, remark, roleId, roleName }
      this.$refs.tree.forEach((tree,index)=>{
        tree.setCheckedKeys(this.roleForm.perms[index])
      })
    },
   async confirmAdd(){
    if (this.roleId) {
    await updateRoleAPI(this.roleForm)
    this.$message({
      type:'success',
      message:'编辑角色成功'
    })
  } else {
    // 添加
    await addRoleAPI(this.roleForm)
  }
  this.$message({
    type: 'success',

        type:'success',
        message:'添加角色成功'
      })


  this.$router.back()


      // this.$router.back()
    },
    async getTreeList(){
      const res=await getTreeListAPI()
      this.treeList=res.data
    },
    next(){
      if(this.activeIndex==3){
        return
      }
      //在第一步的时候应该做一个验证
      if(this.activeIndex==1){
        this.$refs.roleForm.validate(valid=>{
          if(valid){
            this.activeIndex++
          }
        })

      }else if(this.activeIndex==2){
        var flag=0
        this.roleForm.perms=[]
        this.$refs.tree.forEach(item=>{
          // console.log(item.getCheckedKeys())
          // console.log(item.getCheckedNodes());
          this.roleForm.perms.push(item.getCheckedKeys())
          if(item.getCheckedKeys().length>0){
            flag++
            this.activeIndex++

          }
        })
        if(!flag){
          this.$message({
            type:'error',
            message:'请至少勾选一个选项'
          })
        }else{
          this.$refs.disabletree.forEach((item,index)=>{
            item.setCheckedKeys(this.roleForm.perms[index])
          })
        }
      }

    },
    previous(){
      if(this.activeIndex==1){
        return
      }
      this.activeIndex--
    }
  }

}
</script>
<style scoped lang="scss">
.add-role {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    position: fixed;
    top: 64px;
    bottom: 88px;
    width: 100%;
    overflow-y: auto;
    background: #f4f6f8;
    padding: 20px 230px;
    display: flex;
    .step-container {
      height: 400px;
      width: 200px;
    }
    .form-container {
      flex:1;
      background-color: #fff;
      overflow-y: auto;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form {
          display: flex;
          flex-wrap: wrap;
        }
        .info{
          font-size:14px;
          color:#666;
          .form-item{
            margin-bottom:20px;
          }
        }
      }
      .form-box{
        width: 600px;
        display: flex;
        flex-direction: column;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
    z-index: 10001;
  }
  .tree-wrapper {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      .tree-item {
        flex: 1;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }
}
</style>
